package cn.billy.pet.service.impl;

import cn.billy.basic.exception.BusinessException;
import cn.billy.basic.utils.BaiduAuditUtils;
import cn.billy.pet.domain.Pet;
import cn.billy.pet.domain.PetDetail;
import cn.billy.pet.domain.PetOnlineAuditLog;
import cn.billy.pet.domain.PetType;
import cn.billy.pet.mapper.PetDetailMapper;
import cn.billy.pet.mapper.PetMapper;
import cn.billy.pet.mapper.PetOnlineAuditLogMapper;
import cn.billy.pet.mapper.PetTypeMapper;
import cn.billy.pet.service.IPetService;
import cn.billy.basic.service.impl.BaseServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author billy
 * @since 2022-07-16
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

    @Override
    public void save(Pet pet) {
        // 1.保存宠物
        petMapper.add(pet);
        // 2.设置宠物详情关联宠物
        PetDetail detail = pet.getDetail();

        detail.setPetId(pet.getId());
        // 3.保存宠物详情
        petDetailMapper.add(detail);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
    }

    // 宠物批量上架
    @Override
    public void onSalePet(List<Long> ids) {
        // 遍历Ids
        for (Long id : ids) {
            // 1.得到每个宠物基本信息对象
            Pet pet = petMapper.getById(id);
            StringBuffer sbf = new StringBuffer();
            // 2.调用百度AI进行文字，图片的审核
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                sbf.append("宠物名称不合法，宠物名称为：" + pet.getName());
            }
            /*if (!BaiduAuditUtils.ImgCensor("123.207.27.208" + pet.getResources().split(",")[0])){
                sbf.append("宠物图片不合法，宠物图片路径：" + pet.getResources().split(",")[0]);
            }*/
            if (sbf.length() > 0){
                // 2.1.如果违规，我们要保存审核失败日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setNote(sbf.toString());
                petOnlineAuditLogMapper.add(petOnlineAuditLog);
                continue;
            }
            petMapper.onsale(id);
        }

    }

    // 宠物批量下架
    @Override
    public void offSalePet(List<Long> ids) {
        petMapper.offsale(ids);
    }
}
